package logic

import (
	"context"
	"zhongwen/models/mysql"

	"zhongwen/zhongwenrpc/internal/svc"
	"zhongwen/zhongwenrpc/zhongwenrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelShowLogic {
	return &NovelShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelShowLogic) NovelShow(in *zhongwenrpc.NovelShowRequest) (*zhongwenrpc.NovelShowResponse, error) {
	// todo: add your logic here and delete this line
	var noveldata mysql.NovelData
	mysql.DB.Debug().Model(&mysql.Novel{}).Select("novels.*, classes.NAME AS class_name").
		Joins("LEFT JOIN collects ON novels.id = collects.novel_id").
		Joins(" LEFT JOIN classes ON classes.id = novels.class_id").
		Where("novels.id=?", in.NovelId).Find(&noveldata)

	var postList []*zhongwenrpc.NovelList
	for _, data := range postList {
		list := zhongwenrpc.NovelList{
			Title:      data.Title,
			Info:       data.Info,
			CollectNum: data.CollectNum,
			ClassName:  data.ClassName,
			UserName:   data.UserName,
			NovelId:    data.NovelId,
		}
		postList = append(postList, &list)
	}

	return &zhongwenrpc.NovelShowResponse{
		Title:      noveldata.Title,
		Info:       noveldata.Info,
		CollectNum: noveldata.CollectNum,
		ClassName:  noveldata.ClassName,
	}, nil
}
